package com.tj.jinzao.fragment;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseFragment;

import java.lang.reflect.Field;

import butterknife.BindView;

/**
 * 社区首页
 * Created by wjy on 2018/12/11.
 */
public class CommunityFragment extends BaseFragment {

    @BindView(R.id.viewPager)
    ViewPager mViewPaper;
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    private FragmentPagerAdapter mPagerAdapter;
    private int mPrevPosition = -1;
    private Fragment mCurrentFragment;
    private int mPosition = 0;

    public static CommunityFragment newInstance() {
        CommunityFragment fragment = new CommunityFragment();
        Bundle args = new Bundle(1);
        args.putInt(ARG_LAYOUT_RES_ID, R.layout.fragment_community);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupViews(LayoutInflater inflater, View view) {
        super.setupViews(inflater, view);
        setAdapter();
    }

    public void setAdapter() {
        mPagerAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(final int position) {
                return ArticleListFragment.newInstance(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "关注";
                    case 1:
                        return "推荐";
                    case 2:
                        return "热点";
                    case 3:
                        return "视频";
                    default:
                        return "";
                }
            }

            @Override
            public int getCount() {
                return 4;
            }


            @Override
            public void setPrimaryItem(ViewGroup container, int position, Object object) {
                super.setPrimaryItem(container, position, object);
                if (object == null || (mPrevPosition == position && mCurrentFragment != null))
                    return;
                mPrevPosition = position;
                mCurrentFragment = (Fragment) object;
                if (null == mCurrentFragment) return;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                super.destroyItem(container, position, object);
                getChildFragmentManager().beginTransaction()
                        .remove((Fragment) object).commitAllowingStateLoss();
                try {
                    Field parentFragment = Fragment.class.getDeclaredField("mParentFragment");
                    parentFragment.setAccessible(true);
                    parentFragment.set(object, null);
                } catch (NoSuchFieldException ignored) {
                } catch (IllegalAccessException ignored) {
                }
            }
        };
        mViewPaper.setAdapter(mPagerAdapter);
        mViewPaper.setCurrentItem(mPosition);
        mTabLayout.setupWithViewPager(mViewPaper);
    }

    @Override
    public void onStart() {
        super.onStart();
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabLayout, 30, 30);
            }
        });
    }

    public static void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

}